package so.ifl.commons.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since Jun 15, 2010 10:31:43 AM
 */
public class DateUtil {
	
	public static SimpleDateFormat YMD_SDF = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat YMD_SDF_SIMPLE_POINT = new SimpleDateFormat("yy.MM.dd");
	public static SimpleDateFormat YMD_SDF_POINT = new SimpleDateFormat("yyyy.MM.dd");
	public static SimpleDateFormat YMD_SDF_M = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat YMD_SDF_YEAR = new SimpleDateFormat("yyyy");	
	public static SimpleDateFormat YMD_SDF_MONTH = new SimpleDateFormat("MM");
	public static SimpleDateFormat YMD_SDF_DAY = new SimpleDateFormat("dd");
	
	static public Date getDate(){
		Date date = new Date();
		SimpleDateFormat year = new SimpleDateFormat("yyyy");
		String dateyear = year.format(date);
		SimpleDateFormat month = new SimpleDateFormat("MM");
		String datemonth = month.format(date);
		SimpleDateFormat day = new SimpleDateFormat("dd");
		String dateday = day.format(date);
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.parseInt(dateyear), Integer.parseInt(datemonth) - 1, Integer.parseInt(dateday),0, 0, 0);
		return calendar.getTime();
		
	}
	
	static public String getTodayString(){
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		return sf.format(date);
	}
	
	static public Date getFirstSecondInDay(Date date) throws Exception{
		SimpleDateFormat sfyear = new SimpleDateFormat("yyyy-MM-dd");
		String time = sfyear.format(date);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		
		String str = time + " 00:00:00:000";
		Date result = null;
		try {
			result = sf.parse(str);
		} catch (ParseException e) {			
			e.printStackTrace();
			throw new Exception("DateUtil00001");
		}
		return result;
	}
	
	static	public Date getLastSecondInDay(Date date) throws Exception {
		SimpleDateFormat sfyear = new SimpleDateFormat("yyyy-MM-dd");
		String time = sfyear.format(date);
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		
		String str = time + " 23:59:59:000";
		Date result = null;
		try {
			result = sf.parse(str);
		} catch (ParseException e) {			
			e.printStackTrace();
			throw new Exception("DateUtil00001");
		}
		return result;
	}
	
	static	public String getCurrentYear(){
		SimpleDateFormat sfyear = new SimpleDateFormat("yyyy");
		String time = sfyear.format(new Date());
		return time;
	}
	
	static public String getCurrentMonth(){
		SimpleDateFormat sfyear = new SimpleDateFormat("MM");
		String time = sfyear.format(new Date());
		return time;
	}
	
}
